package C50;

import java.util.Arrays;

/**
 * ClassName: C01
 * Package: C50
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/27 17:52
 * @Version 1.0
 * @Since 1.0
 */
public class C01 {
    public static void main(String[] args) {
        int[] arr = {4,2,5,7};
        sortArrayByParityII(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static int[] sortArrayByParityII(int[] nums) {
        int even = 0, odd = 1;
        int n = nums.length;
        for (int i = 0; even < nums.length && odd < nums.length; i++) {
            if((nums[n - 1] & 1) == 1) {
                swap(nums, odd, n - 1);
                odd += 2;
            } else {
                swap(nums, even, n - 1);
                even += 2;
            }
        }
        return nums;
    }

    private static void swap(int[] nums, int odd, int n) {
        if(odd == n) {
            return;
        }
        nums[odd] = nums[odd] ^ nums[n];
        nums[n] = nums[n] ^ nums[odd];
        nums[odd] = nums[odd] ^ nums[n];
    }
}
